package com.mc.lp.database.entity.helper;

import org.apache.log4j.Logger;

import com.mc.lp.database.entity.AttachmentEntity;
import com.mc.lp.database.entity.type.RecordType;
import com.mc.lp.shared.vo.AttachmentVO;
import com.mc.lp.shared.vo.FileVo;

public class AttachmentEntityHelper implements IEntityHelper<AttachmentEntity, AttachmentVO> {
	Logger log=Logger.getLogger(AttachmentEntityHelper.class);
	@Override
	public AttachmentVO getVO(AttachmentEntity entity) {
		AttachmentVO vo = new AttachmentVO();
		FileVo fileVo = new FileVo(entity.getId(), entity.getName(),null);
		fileVo.setData(null);
//		try {
//			fileVo.setSize(entity.getData().\);
//		} catch (SQLException e) {
//			log.error("error in fetching blob size in attachment entity");
//		}
		fileVo.setDate(String.valueOf(entity.getModifiedDate()));
		vo.setGradeId(entity.getGradeId());
		vo.setRecordId(entity.getRecordId());
		vo.setRecordType(RecordType.getType(entity.getRecordTypeId()).toString());
		vo.setStudentEntityId(entity.getEntityId().getId());
		vo.setTermId(entity.getTermId());
		vo.setYear(entity.getYear());
		vo.setFile(fileVo);
		return vo;
	}

	@Override
	public AttachmentEntity getEntity(AttachmentVO vo) {
		AttachmentEntity entity = new AttachmentEntity();
		setEntityCommonValues(vo,entity);
		return entity;
	}

	@Override
	public void setEntity(AttachmentVO vo, AttachmentEntity entity) {
		setEntityCommonValues(vo,entity);
	}
	
	private void setEntityCommonValues(AttachmentVO vo, AttachmentEntity entity){
		FileVo fileVo = vo.getFile();
		entity.setName(fileVo.getName());
		//entity.setData(fileVo.getData().getBytes());
		entity.setGradeId(vo.getGradeId());
		entity.setRecordId(vo.getRecordId());
		entity.setRecordTypeId(RecordType.getId(vo.getRecordType()));
		entity.setTermId(vo.getTermId());
		entity.setYear(vo.getYear());
	}
}